package com.atgm.mall.gmmailorder.listener;

import com.atgm.mall.gmmailorder.service.OrderService;
import com.atgm.common.to.PayOrderVo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 订单支付
 */
@Slf4j
@Service
public class OrderPayListener {

    @Autowired
    private OrderService orderService;

    @RabbitListener(queues = "pay.order.release.stock.queue")
    public void updateOrderPay(PayOrderVo payOrderVo, Channel channel, Message message) {
        try {
            //ACK确认机制
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            orderService.updateOrderPay(payOrderVo);
        }catch (Exception e) {
            try {
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
            } catch (IOException ex) {
                log.error("订单支付消息消费失败");
            }
        }
    }

}
